package cn.gbase.jiangsu.data.transfer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;

import cn.gbase.jiangsu.data.transfer.bean.Product;
import cn.gbase.jiangsu.data.transfer.bean.ProductSortTreeNode;
import cn.gbase.jiangsu.data.transfer.bean.Supplier;
import cn.gbase.jiangsu.data.transfer.service.ProductService;
import cn.gbase.jiangsu.data.transfer.service.ProductSortTreeNodeService;
import cn.gbase.jiangsu.data.transfer.service.SupplierService;

@IocBean
@At("/product")
@Filters(@By(type = CheckSession.class, args = { "user", "/" }))
public class ProductAction {

	@Inject
	private ProductService productService;

	@Inject
	private ProductSortTreeNodeService productSortTreeNodeService;

	@Inject
	private SupplierService supplierService;

	/**
	 * 分页查询商品（商品管理页面）.
	 * 
	 * @param currentPage
	 * @param groupId
	 * @param addTime
	 * @param keyword
	 * @param req
	 */
	@At("/listProduct")
	@Ok("jsp:/product/listProduct")
	public QueryResult listProduct(@Param("currentPage") int currentPage, @Param("id") String id,
			@Param("keyword") String keyword, @Param("nodeId") String nodeId,
			@Param("selectedNodeName") String selectedNodeName, @Param("passStatus") String passStatus,
			@Param("supplierId") String supplierId, HttpServletRequest request) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("id", id);
		itemMap.put("keyword", keyword);
		itemMap.put("nodeId", nodeId);
		itemMap.put("selectedNodeName", selectedNodeName);
		itemMap.put("passStatus", passStatus);
		itemMap.put("supplierId", supplierId);
		request.setAttribute("formdata", itemMap);

		// 商品分类
		String json = productService.getTreeJsonDataForZTree(1);
		request.setAttribute("treedata", json);

		// 供应商列表
		List<Supplier> supplierList = supplierService.getSupplierList();
		request.setAttribute("supplierList", supplierList);
		List<Integer> supplierIds=new ArrayList<Integer>();
		for(Supplier supplier:supplierList)
			supplierIds.add(supplier.getId());
		return productService.getProductList(currentPage, id, keyword, nodeId, passStatus, supplierIds);

	}

	/**
	 * 跳转到添加商品页面（商品管理）.
	 */
	@At("/toAddProduct")
	@Ok("jsp:/product/editProduct")
	public void toAddProduct(HttpServletRequest req) {

	}

	/**
	 * 跳转到修改商品页面.
	 */
	@At("/toEditProduct")
	@Ok("jsp:/product/editProduct")
	public void toEditProduct(@Param("id") int id, HttpServletRequest req) {

		// 要修改的商品
		Product p = productService.getProductById(id);
		p.setNodeId(p.getProductSort());
		ProductSortTreeNode node = productSortTreeNodeService.getStandTreeNode(p.getProductSort());
		if (node != null) {
			p.setSelectedNodeName(node.getName());
		}
		req.setAttribute("formdata", p);

		// 商品分类
		String json = productService.getTreeJsonDataForZTree(1);
		req.setAttribute("treedata", json);
	}

	/**
	 * 保存商品.
	 * 
	 * @param sp
	 */
	@At("/saveProduct")
	@POST
	@Ok(">>:/product/listProduct")
	public void saveProduct(@Param("..") Product product) {
		product.setProductSort(product.getNodeId());
		productService.updateProduct(product);
	}

	/**
	 * 审核通过商品.
	 * 
	 * @return
	 */
	@At("/pass")
	@Ok("json")
	public String pass(@Param("id") int id) {

		productService.passProduct(id);
		return "{id:0}";
	}

	/**
	 * 审核通过多个商品.
	 * 
	 * @return
	 */
	@At("/passMany")
	@Ok("json")
	public String passMany(@Param("idStr") String idStr) {

		String[] idArray = idStr.split(",");
		for (String id : idArray) {
			productService.passProduct(Integer.valueOf(id));
		}
		return "{id:0}";
	}

	/**
	 * 删除商品（逻辑删除？）.
	 */
	@At("/delete")
	@Ok("json")
	public String delete(@Param("id") int id) {
		productService.deleteProduct(id);
		return "{id:0}";
	}

	/**
	 * 删除商品（逻辑删除？）.
	 */
	@At("/deleteMany")
	@Ok("json")
	public String deleteMany(@Param("idStr") String idStr) {
		String[] idArray = idStr.split(",");
		for (String id : idArray) {
			productService.deleteProduct(Integer.valueOf(id));
		}
		return "{id:0}";
	}

}
